// pages/socialSystem/socialList/socialList.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        articalList: [],
        articalType: 0,
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getArticalList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function(options) {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            articalList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getArticalList()
    },

    getArticalList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let articalList = this.data.articalList
        let data = {
            activity: 785,
            type: this.data.articalType,
            limit: 5,
            order_by: 'add_time',
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Moments/moment_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    articalList: articalList,
                    isAll: true
                })
            } else {
                articalList = articalList.concat(res)
                that.setData({
                    articalList: articalList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.articalType == e.currentTarget.dataset.type) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            articalType: e.currentTarget.dataset.type,
            nowPage: 0,
            isAll: false,
            isLoading: false,
            articalList: []
        })
        this.getArticalList()
    },

    praiseClick: function(e) {
        const that = this
        let articalList = this.data.articalList
        if (e.currentTarget.dataset.status == 1) {
            return
        }
        let data = {
            activity: 785,
            moments_id: e.currentTarget.dataset.id
        }
        common.post('Moments/moment_like', data).then(function(res) {
            articalList.forEach(function(item) {
                if (item.id == e.currentTarget.dataset.id) {
                    item.is_like = 1
                    item.praise_num = 1 + parseInt(item.praise_num)
                }
            })
            that.setData({
                articalList: articalList
            })
        }).catch(function(e) {})
    },

    goArticalDetail: function(e) {
        wx.navigateTo({
            url: '/pages/socialSystem/articalDetail/articalDetail?articalId=' + e.currentTarget.dataset.id + '&articalType=' + this.data.articalType
        })
    },

    goWriteArtical: function() {
        wx.navigateTo({
            url: '/pages/socialSystem/writeArtical/writeArtical'
        })
    }
})